#pragma once

#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <cstdio>

//用于测试
#define PATH "."
#define FILENAME "fifo"

#define ERR_EXIT(m)         \
    do                      \
    {                       \
        perror(m);          \
        exit(EXIT_FAILURE); \
    } while (0)





class NameFifo
{
public:
    NameFifo(const std::string &path, const std::string &name)
        : _path(path), _name(name)
    {
        _fifopath = _path + "/" + _name;

        // 新建管道
        umask(0);
        int n = mkfifo(_fifopath.c_str(), 0666);
        if (n < 0)
        {
            // std::cerr << "error mkfifo" << std::endl;
            ERR_EXIT("mkfifo");
        }
        else
        {
            std::cout << "mkfifo success" << std::endl;
        }
    }

    ~NameFifo()
    {
        int n = unlink(_fifopath.c_str());
        if (n == 0)
        {
            // std::cerr << "error unlink" << std::endl;
            // 宏定义 更方便
            ERR_EXIT("unlink");
        }
        else
        {
            std::cout << "remove fifo failed" << std::endl;
        }
    }

private:
    std::string _path;
    std::string _name;
    std::string _fifopath;
};

class FileOper
{
public:
    FileOper(const std::string& path, const std::string& name)
    :_path(path)
    ,_name(name)
    ,_fd(-1)
    {
        _fifoname = _path + "/" + _name;
    }

    void OpenForRead()
    {
        // 打开, write 方没有执行open的时候，read方，就要在open内部进行阻塞
        // 直到有人把管道文件打开了，open才会返回！

        // 如果当前打开操作是为读⽽打开FIFO时 
        // O_NONBLOCK disable：阻塞直到有相应进程为写⽽打开该FIFO 
        // O_NONBLOCK enable：⽴刻返回成功 
        _fd = open(_fifoname.c_str(), O_RDONLY);
        if(_fd < 0)
        {
            ERR_EXIT("open");
        }   
        
        std::cout << "open fifo success" << std::endl;
    }

    void OpenForWrite()
    {
        //如果当前打开操作是为写⽽打开FIFO时 
        //O_NONBLOCK disable：阻塞直到有相应进程为读⽽打开该FIFO 
        //O_NONBLOCK enable：⽴刻返回失败，错误码为ENXIO
        _fd = open(_fifoname.c_str(), O_WRONLY);
        if(_fd < 0)
        {
            ERR_EXIT("open");
        }

        std::cout << "open fifo success" << std::endl;
    }

    void Write()
    {
        //写入操作
        std::string massage;
        int cnt = 1;
        pid_t id = getpid();
        while(true)
        {
            std::cout << "Please Enter# ";
            std::getline(std::cin, massage);
            //方便查看
            massage += (", message: umber: " + std::to_string(cnt++) + ", [" + std::to_string(id) + "]");

            write(_fd, massage.c_str(), massage.size());
        }
    }

    void Read()
    {
        while(true)
        {
            char buffer[1024];
            int n = read(_fd, buffer, sizeof(buffer)-1);
            
            //std::cout << buffer << std::endl;
            if(n > 0)
            {
                //std::cout << 2 << std::endl;
                buffer[n] = 0; ///?????/
                //std::cout << "Client say# " << buffer << std::endl;
                std::cout << "Client Say# " << buffer << std::endl;
            }
            else if(n == 0)
            {
                std::cout << "client quit! me too!" << std::endl;
                break;
            }
            else
            {
                std::cerr << "read error" << std::endl;
                break;
            }
        }
    }

    void Close()
    {
        if(_fd > 0)
        close(_fd);
    }
    ~FileOper()
    {
    }

private:
    std::string _name;
    std::string _path;
    std::string _fifoname;
    int _fd;
};